#include <iostream>
using namespace std;
// g++ -o test_explicit test_explicit.cc
class Complex {
 public:
  Complex() {
    real = 0;
    imag = 0;
  }
  Complex(double r, double i) {
    real = r;
    imag = i;
  }
  operator double() { return real; }  //类型转换函数
 private:
  double real;
  double imag;
};
void test() {
  Complex c1(3, 4), c2(5, -10), c3;
  double d;
  d = 2.5 + c1;  // 这里调用了类型转换函数
  cout << d << endl;
  return;
}

class A {
 public:
  int a = 0;
  int b = 0;
  A() {}
  // explicit A(int c, int d = 20) : a(c), b(d) {}
  // explicit修饰单参构造函数，这里的第二个参数有默认参数，所以也可视为单参，阻止隐式构造
  A(int c, int d = 20) : a(c), b(d) {}
  A(char a) { this->a = a; }  // 转换构造函数
  void show() { cout << a << " " << b << endl; }
};

int main() {
  Complex c1(3, 4), c2(5, -10), c3;
  double d;
  d = 2.5 + c1;  // 这里调用了类型转换函数
  cout << d << endl;

  A a1;
  a1.show();
  a1 = 1;  // 发生了隐式构造
  a1.show();
  A a2('c');
  a2.show();
  return 0;
}